/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Opt;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.FreshAirVentilator;
import me.zhengjie.modules.biz.service.FreshAirVentilatorService;
import me.zhengjie.modules.biz.service.dto.ExhaustFanDto;
import me.zhengjie.modules.biz.service.dto.FreshAirVentilatorDto;
import me.zhengjie.modules.biz.service.dto.FreshAirVentilatorQueryCriteria;
import me.zhengjie.modules.biz.utils.NettyServerHandler;
import me.zhengjie.service.vo.R;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-17
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "fresh-air-ventilator管理")
@RequestMapping("/api/freshAirVentilator")



public class FreshAirVentilatorController {

    private final FreshAirVentilatorService freshAirVentilatorService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('freshAirVentilator:list')")
    public void exportFreshAirVentilator(HttpServletResponse response, FreshAirVentilatorQueryCriteria criteria) throws IOException {
        freshAirVentilatorService.download(freshAirVentilatorService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询fresh-air-ventilator")
    @ApiOperation("查询fresh-air-ventilator")
//    @PreAuthorize("@el.check('freshAirVentilator:list')")
    public ResponseEntity<Object> queryFreshAirVentilator(FreshAirVentilatorQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(freshAirVentilatorService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增fresh-air-ventilator")
    @ApiOperation("新增fresh-air-ventilator")
//    @PreAuthorize("@el.check('freshAirVentilator:add')")
    public ResponseEntity<Object> createFreshAirVentilator(@Validated @RequestBody FreshAirVentilator resources){
        return new ResponseEntity<>(freshAirVentilatorService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改fresh-air-ventilator")
    @ApiOperation("修改fresh-air-ventilator")
//    @PreAuthorize("@el.check('freshAirVentilator:edit')")
    public ResponseEntity<Object> updateFreshAirVentilator(@Validated @RequestBody FreshAirVentilator resources){
        freshAirVentilatorService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除fresh-air-ventilator")
    @ApiOperation("删除fresh-air-ventilator")
//    @PreAuthorize("@el.check('freshAirVentilator:del')")
    public ResponseEntity<Object> deleteFreshAirVentilator(@RequestBody Integer[] ids) {
        freshAirVentilatorService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/3d/fresh-air-ventilator")
    @Log("查询fresh-air-ventilator")
    @ApiOperation("查询fresh-air-ventilator")
    @AnonymousAccess
    public ResponseEntity<Object> queryFreshAirVentilatorFor3d(FreshAirVentilatorQueryCriteria criteria){
        return new ResponseEntity<>(freshAirVentilatorService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping("/3d/fresh-air-ventilator-floor")
    @Log("查询fresh-air-ventilator-floor")
    @ApiOperation("查询fresh-air-ventilator-floor")
    @AnonymousAccess
    public ResponseEntity<Object> queryFreshAirVentilatorFloorFor3d(@RequestParam Integer floor){
        return new ResponseEntity<>(freshAirVentilatorService.queryFreshAirVentilatorFloor(floor),HttpStatus.OK);
    }

    @PostMapping("/open")
//    @Log("控制新风机启动")
    @ApiOperation("控制新风机启动")
    public ResponseEntity<Object> openFreshAirVentilator(@RequestParam int id){
        freshAirVentilatorService.openFreshAirVentilator(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/close")
//    @Log("控制新风机停止")
    @ApiOperation("控制新风机停止")
    public ResponseEntity<Object> closeFreshAirVentilator(@RequestParam int id){
        freshAirVentilatorService.closeFreshAirVentilator(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/control1")
//    @Log("新风机启停1")
    @ApiOperation("新风机启停1")
    @AnonymousAccess
    public ResponseEntity<Object> control1(){
        return new ResponseEntity<>(freshAirVentilatorService.control(1),HttpStatus.OK);
    }

    @GetMapping("/control2")
//    @Log("新风机启停2")
    @ApiOperation("新风机启停2")
    @AnonymousAccess
    public ResponseEntity<Object> control2(){
        return new ResponseEntity<>(freshAirVentilatorService.control(2),HttpStatus.OK);
    }

    @GetMapping("/control3")
//    @Log("新风机启停3")
    @ApiOperation("新风机启停3")
    @AnonymousAccess
    public ResponseEntity<Object> control3(){
        return new ResponseEntity<>(freshAirVentilatorService.control(3),HttpStatus.OK);
    }

    @GetMapping("/control4")
//    @Log("新风机启停4")
    @ApiOperation("新风机启停4")
    @AnonymousAccess
    public ResponseEntity<Object> control4(){
        return new ResponseEntity<>(freshAirVentilatorService.control(4),HttpStatus.OK);
    }

    @PostMapping("/fresh-air-ventilator-temp1")
//    @Log("新风机1读取温度")
    @ApiOperation("新风机1读取温度")
    @AnonymousAccess
    public ResponseEntity<Object>freshAirVentilatorTemp1(@RequestBody Map<String,Object> temperature){
        int indoorTemperature = 0;
        int ambientTemperature= 0;
        if (Objects.nonNull(temperature.get("indoor"))) {
            indoorTemperature = (int) temperature.get("indoor");
        }
        if (Objects.nonNull(temperature.get("ambient"))) {
            ambientTemperature = (int) temperature.get("ambient");
        }
//        String errorOne = (String) temperature.get("errorOne");
//        String errorTwo = (String) temperature.get("errorTwo");
//        String errorThree = (String) temperature.get("errorThree");
//        return new ResponseEntity<>(freshAirVentilatorService.freshAirVentilatorTemp(1,indoorTemperature,ambientTemperature),HttpStatus.OK);
        return new ResponseEntity<>(freshAirVentilatorService.freshAirVentilatorTemp(1,indoorTemperature,ambientTemperature,null,null,null),HttpStatus.OK);
    }

    @PostMapping("/fresh-air-ventilator-temp2")
//    @Log("新风机2读取温度")
    @ApiOperation("新风机2读取温度")
    @AnonymousAccess
    public ResponseEntity<Object>freshAirVentilatorTemp2(@RequestBody Map<String,Object> temperature){
        int indoorTemperature = 0;
        int ambientTemperature= 0;
        if (Objects.nonNull(temperature.get("indoor"))) {
            indoorTemperature = (int) temperature.get("indoor");
        }
        if (Objects.nonNull(temperature.get("ambient"))) {
            ambientTemperature = (int) temperature.get("ambient");
        }
//        String errorOne = (String) temperature.get("errorOne");
//        String errorTwo = (String) temperature.get("errorTwo");
//        String errorThree = (String) temperature.get("errorThree");
        return new ResponseEntity<>(freshAirVentilatorService.freshAirVentilatorTemp(2,indoorTemperature,ambientTemperature,null,null,null),HttpStatus.OK);
    }

    @PostMapping("/fresh-air-ventilator-temp3")
//    @Log("新风机3读取温度")
    @ApiOperation("新风机3读取温度")
    @AnonymousAccess
    public ResponseEntity<Object>freshAirVentilatorTemp3(@RequestBody Map<String,Object> temperature){
        int indoorTemperature = 0;
        int ambientTemperature= 0;
        if (Objects.nonNull(temperature.get("indoor"))) {
            indoorTemperature = (int) temperature.get("indoor");
        }
        if (Objects.nonNull(temperature.get("ambient"))) {
            ambientTemperature = (int) temperature.get("ambient");
        }
//        String errorOne = (String) temperature.get("errorOne");
//        String errorTwo = (String) temperature.get("errorTwo");
//        String errorThree = (String) temperature.get("errorThree");
        return new ResponseEntity<>(freshAirVentilatorService.freshAirVentilatorTemp(3,indoorTemperature,ambientTemperature,null,null,null),HttpStatus.OK);
    }

    @PostMapping("/fresh-air-ventilator-temp4")
//    @Log("新风机4读取温度")
    @ApiOperation("新风机4读取温度")
    @AnonymousAccess
    public ResponseEntity<Object>freshAirVentilatorTemp4(@RequestBody Map<String,Object> temperature){
        int indoorTemperature = 0;
        int ambientTemperature= 0;
        if (Objects.nonNull(temperature.get("indoor"))) {
            indoorTemperature = (int) temperature.get("indoor");
        }
        if (Objects.nonNull(temperature.get("ambient"))) {
            ambientTemperature = (int) temperature.get("ambient");
        }
//        String errorOne = (String) temperature.get("errorOne");
//        String errorTwo = (String) temperature.get("errorTwo");
//        String errorThree = (String) temperature.get("errorThree");
        return new ResponseEntity<>(freshAirVentilatorService.freshAirVentilatorTemp(4,indoorTemperature,ambientTemperature,null,null,null),HttpStatus.OK);
    }

    @GetMapping("/sendMsgToClient")
    @AnonymousAccess
//    @Log("启停")
    @ApiOperation("启停")
    public ResponseEntity<Object> sendMsgToClient(Integer id,Integer isOpen){
        return new ResponseEntity<>(freshAirVentilatorService.sendMsgToClient(id,isOpen),HttpStatus.OK);
    }

}
